/**
 * @param {Array} position = [[120,30],[120,40],[120,20]] 
 * @param {Array} attr = [
 * {name:"name",type:"string",value:"中国"},
 * {name:"id",type:"number",value:1001}
 * ]
 * @param {Object} service ={name:"igserver服务的名称",layerId:"添加要素所在的图层"}
 * @param {Object} docLayer 地图文档的图层
 */
const AREA_STYLE = {
    EndColor: 1,
    FillColor: 11,  //填充色
    FillMode: 0,
    OutPenWidth: 1,
    OverMethod: 0,
    PatAngle: 1,
    PatColor: 1,
    PatHeight: 1,
    PatID: 0,//填充的图案
    PatWidth: 1
}
class Area {
    static add({
        position,
        attr,
        service,
        docLayer
    }) {
        /*1、创建一个mapgis的要素 = geometry+style+attr */
        var pointObj = position.map(item => new Zondy.Object.Point2D(item[0], item[1]))
        //设置区要素的几何信息
        var gArc = new Zondy.Object.Arc(pointObj);
        //构成区要素折线
        var gAnyLine = new Zondy.Object.AnyLine([gArc]);
        //构成区要素
        var gRegion = new Zondy.Object.GRegion([gAnyLine]);
        //构成区要素的几何信息
        var fGeom = new Zondy.Object.FeatureGeometry({ RegGeom: [gRegion] });

        //设置区要素的图形参数信息
        var cRegionInfo = new Zondy.Object.CRegionInfo(AREA_STYLE);
        //要素图形参数信息
        var graphicInfo = new Zondy.Object.WebGraphicsInfo({
            InfoType: 3,
            RegInfo: cRegionInfo
        });
        //设置区要素的属性信息
        var attValue = attr.map(item => item.value);
        //创建一个新的区要素
        var newFeature = new Zondy.Object.Feature({
            AttValue: attValue,
            fGeom: fGeom,
            GraphicInfo: graphicInfo
        });
        newFeature.setFType(3);
        /* 2、创建要素集--映射mapgis图层 添加要素 */
        var featureSet = new Zondy.Object.FeatureSet();

        var fldNumber = attr.length;
        var fldType = attr.map(item => item.type);
        var fldName = attr.map(item => item.name);
        var cAttValue = new Zondy.Object.CAttStruct({ FldNumber: fldNumber, FldType: fldType, FldName: fldName });
        featureSet.AttStruct = cAttValue;

        featureSet.addFeature(newFeature);
        /* 3、调用服务 */
        var editDocFeature = new Zondy.Service.EditDocFeature(service.name, service.layerId, {

        });
        editDocFeature.add(featureSet, this.onSuccess(docLayer));
    }
    static onSuccess(docLayer) {
        return function (data) {
            if (data.succeed) {
                alert("添加区要素成功！");
                docLayer.refresh();
            } else {
                alert("添加区要素失败！");
            }
        }
    }
}